﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace KalturaNWeb.Pages
{
    public partial class Default : System.Web.UI.Page
    {
        #region members
        public String _appPartID = String.Empty;
        #endregion

        #region properties

        public String HostWebURL
        {
            get
            {
                try
                {
                    return Request.QueryString["SPHostUrl"].ToString();
                }
                catch (Exception)
                {
                    return String.Empty;
                }
            }
        }

        public String AccessToken
        {
            get
            {
                try
                {
                    return Session[this._appPartID + "_AccessToken"].ToString();
                }
                catch (Exception)
                {
                    return String.Empty;
                }
            }
            set
            {
                Session[this._appPartID + "_AccessToken"] = value;
            }
        }

        #endregion

        #region events
        protected void Page_PreInit(object sender, EventArgs e)
        {
            this._appPartID = Request.Params["wpId"];
        }

        //protected void Page_Load(object sender, EventArgs e)
        //{
        //    //set app part id
        //    

        //    SetAuthenticationParameters();
        //}
        #endregion


        #region methods

        /// <summary>
        /// Set Access Token for App
        /// </summary>
        //public void SetAuthenticationParameters()
        //{
        //    try
        //    {
        //        var contextToken = TokenHelper.GetContextTokenFromRequest(Page.Request);

        //        if (String.IsNullOrEmpty(AccessToken))
        //        {
        //             check if context token is not null
        //            if (!String.IsNullOrEmpty(contextToken) && !String.IsNullOrEmpty(HostWebURL))
        //            {
        //                SharePointContextToken spContextToken = TokenHelper.ReadAndValidateContextToken(contextToken, Request.Url.Authority);
        //                AccessToken = TokenHelper.GetAccessToken(spContextToken, new Uri(HostWebURL).Authority).AccessToken;
        //            }
        //            else if (!IsPostBack)
        //            {
        //                throw new Exception("Therse was some error. Please access the app from SharePoint site again");
        //            }
        //        }
        //    }
        //    catch (Exception)
        //    {
        //    }
        //}

        #endregion
    }
}